#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "../include/wrap.h"
#include "../include/clientAnalysis.h"
#include "../include/draw.h"
#include <netinet/in.h>
#include <string.h>


#define MAXLEN 4000
#define SERVPORT 8000

int main(void) {
	
	struct sockaddr_in servaddr;
	char buf[MAXLEN];	
	char identity[10];
	char acount[10];
	char name[20];
	char password[20];
	char send[50];
	int sockfd;
	int n;
	int flag = -1;
			
	sockfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERVPORT);	
	servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	Connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

	initHome();

	while (1) {

		printf("please entry 1||2||3:");
		int c;
		c = getchar();

		if (c == '1'){
		
			initLogin(&flag, identity, acount, password);

			char *argv[4] = {"login", identity, acount, password};
			strspl(send, 4, argv);
			Write(sockfd, send, strlen(send));								//send cmd to servce
		
			n = Read(sockfd, buf, MAXLEN);									//ack serve back 
			if (n == -1){
				printf("\nthe servce is closed\n");
				Close(sockfd);
			} else {
				if(buf[6] == '1'){
					buf[n] = '\0';
					strcpy(name, &buf[8]);
					break;
				} else {
					printf("\nacount or password is not right\n");
				}
			}
		
  		} else if (c == '2'){

			initRegister(&flag, identity, name, password);
	
			char *argv[4] = {"register", identity, name, password};
			strspl(send, 4, argv);
			Write(sockfd, send, strlen(send));								//send cmd to servce
		
			n = Read(sockfd, buf, MAXLEN);									//ack serve back 
			if (n == -1){
				printf("the servce is closed\n");
				Close(sockfd);
			} else {
				if(buf[9] == '1'){
					printf("\nRegister success !\n");
					printf("your acount is %s\n",&buf[11]);
				} else {
					printf("\nRegister failed\n");
				}
			}	
		} else if (c == '3'){
			exit(0);
		} 
	}
	


	printf("\nLogin success\n");
	initEnterPage(identity, acount, name);
	n = fork();

	if (n == -1){
		printf("fork error\n");
		exit(0);
	} else if (n != 0){
		
		initCmd(identity);
		sendCmd(sockfd, identity, acount, name);						//send msg
	} else {

		while ((n = Read(sockfd, buf, MAXLEN)) != -1 ){
			
			if (buf[0] == 's' && buf[1] == 'e' && buf[2] == 'n'){
				if (buf[5] != '2'){

					if (buf[7] == 'c' && buf[8] == 'l' && buf[9] == 'e'){
						system("clear");
						initCmd(identity);
						continue;
					}
					printf("\n***** your get a msg ******\n");
					write(STDOUT_FILENO, &buf[7], n-7);
					continue;
				}
				continue;
			}

			if (buf[0] == 'o' && buf[1] == 'f' && buf[2] == 'f'){				//offline msg
				
				char ostr[30] = {"\nyou have off-msg:\n"};
				char str[30] = {"\nentry the number:"};
		
				if(buf[7] != 'y'){
					write(STDOUT_FILENO, ostr, strlen(ostr));
				}
				write(STDOUT_FILENO, &buf[7], n-7);
				write(STDOUT_FILENO, str, strlen(str));
				
				continue;
			}

			if (buf[0] == 'a' && buf[1] == 's' && buf[2] == 's'){
				
				char str1[30] = {"\nassr Book:"};
				char str2[30] = {"\nentry the number:"};

				write(STDOUT_FILENO, str1, strlen(str1));
				write(STDOUT_FILENO, &buf[4], n-4);
				write(STDOUT_FILENO, str2, strlen(str2));
				
				continue;
				
			}

			if (buf[0] == 's' && buf[1] == 'e' && buf[2] == 'a' && buf[3] == 'r'){
				
				char str1[30] = {"\nchat record:\n"};
				char str2[30] = {"\nentry the number:"};

				write(STDOUT_FILENO, str1, strlen(str1));
				write(STDOUT_FILENO, &buf[7], n-7);
				write(STDOUT_FILENO, str2, strlen(str2));
				
				continue;
			
			} 
			
			if (buf[0] == '2' && buf[4] == 's'){
				
				write(STDOUT_FILENO, buf, n);	
				char str2[30] = {"\nentry the number:"};
				write(STDOUT_FILENO, str2, strlen(str2));
				buf[3] = '\0';

				char path[30] = {"../customer/"};
				strcat(path, acount);
				strcat(path, "/svc");
				FILE *fp = fopen(path, "w");
				fputs(buf, fp);
				fclose(fp);
				continue;
			}
			char str[30] = {"\nentry the number:"};
			printf("\nfrom service:\n");
			write(STDOUT_FILENO, buf, n);							//rsv msg and analysis
			write(STDOUT_FILENO, str, strlen(str));
		}
		printf("the servce is closed\n");
		Close(sockfd);
		
	}

	return 0;
}
